/**
 * 
 */
package cn.hg.solon.youcan.flex.listener;

import java.util.function.Supplier;

import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.Solon;

import com.mybatisflex.annotation.InsertListener;

import cn.hg.solon.youcan.common.constant.WebConstants;

/**
 * @author 胡高
 *
 */
public class EntityInsertListener implements InsertListener {

    @Override
    public void onInsert(Object entity) {
        Supplier<Integer> getter = Solon.context().getBean(WebConstants.USER_ID_GETTER);
        if (ObjUtil.isNull(getter)) {
            return;
        }
        try {
            BeanUtil.setFieldValue(entity, "creator", getter.get());
            BeanUtil.setFieldValue(entity, "createdDatetime", DateUtil.now());
            BeanUtil.setFieldValue(entity, "del", Boolean.FALSE);
        } catch (Exception e) {
        }
    }

}
